<?php
/**
 *
 *
 */
class Yz_Action_DeleteItem
    extends Yz_Action_Delete
{

    /**
     *  The item to Delete
     *  @var Item
     */
    protected $item;


    /************************************
     *          Public methods          *
     ************************************/

    /**
     *  @param mixed $item
     *  @return this
     */
    public function setItem($item)
    {
        if (! ($item instanceof Yz_Item_Interface))
            throw Yz_Action_Exception::_new('Item must be instance of Yz_Item_Interface');
        if (! $item->id)
            throw Yz_Action_Exception::_new('Item must have a non-empty id');
        $this->item = $item;
        // prevent the action instance from returning results after changing parameters
        $this->executed = false;
        return $this;
    }

    /************************************
     *        Protected methods         *
     ************************************/

    /** Verify arguments before building query
     *      prevent deletion without sensible arguments
     *  @return this
     */
    protected function verifyArguments()
    {
        debug();
        if (! $this->item)
            // assert that if values are non-empty, the fields have been set, too (@see identifyValues)
            throw new Yz_Action_Exception("No item to delete");
        return $this;
    }


    /**
     *  Apply the arguments to the SQL statement
     *      Bind the id argument
     *  @param Yz_SQL_Statement $sql
     *  @return this
     */
    protected function applyArguments(Yz_SQL_Statement $sql)
    {
        debug();
        $sql
            ->where($this->model->getName() . ".id=:_id")
            ->bind("_id", $this->item->id)
        ;
        return $this;
    }

}
